// void, Obj me, Obj target

Unit tgt;
Hero this;
str class;
point pt;
ObjList ol;

Sleep(30);
this = me.AsHero;

if(!.IsValid) return;
tgt = target.AsUnit;
class = tgt.class;
pt = tgt.posRH;
//pr("hero attack u t");
.FormSetupAndMoveTo (pt, 0, 0, false);
// Wait for army to exit
while(!.IsArmyOutside())
	.FormKeepMoving(500);
while(.HasPath())
{
	if(tgt.IsAlive)
	{
		pt = tgt.posRH;
	}
	if(.DistTo(pt) < .sight)
	{
		//.pr("hero::move:an enemy near destination");
		if(!tgt.IsAlive)
		{
			ol = Intersect(ObjsInSight(this, class), EnemyObjs(.player, class)).GetObjList;
			if(ol.count > 0)
			{
				tgt = ol[0];
			}
		}
		if(tgt.IsAlive)
		{
			Sleep(rand(200)+100);
			//pr("tgt alive");
			.AddCommand(true, "engage_unit_type", tgt);
		} else
		{
			Sleep(rand(200)+100);
			.AddCommand(true, "engage");
		}
		return;
	}
	.FormKeepMoving (1500);
}	
